package com.zdp.order.service;

import com.zdp.common.domain.Items;
import com.zdp.common.domain.ItemsSpec;
import com.zdp.common.domain.OrderItems;
import com.zdp.common.domain.Orders;
import com.zdp.common.domain.bo.SubmitItemSpecBO;
import com.zdp.common.domain.bo.SubmitOrderBO;
import com.zdp.common.enums.YesOrNo;
import com.zdp.order.mapper.ItemsMapper;
import com.zdp.order.mapper.ItemsSpecMapper;
import com.zdp.order.mapper.OrderItemsMapper;
import com.zdp.order.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author sesshomaru
 * @date 2021/7/10 17:53
 */
@Slf4j
@Service
public class OrderMainService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderItemsMapper orderItemsMapper;

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private Sid sid;

    @Transactional(rollbackFor = Exception.class)
    public Object createOrder(SubmitOrderBO submitOrderBO) {
        String userId = submitOrderBO.getUserId();
        // 购买的所有商品规格
        List<SubmitItemSpecBO> itemSpecs = submitOrderBO.getItemSpecs();
        Integer payMethod = submitOrderBO.getPayMethod();
        String leftMsg = submitOrderBO.getLeftMsg();
        // 包邮费用设置为0
        Integer postAmount = 0;

        String orderId = sid.nextShort();

        Orders newOrder = new Orders();
        newOrder.setId(orderId);
        newOrder.setUserId(userId);

        newOrder.setReceiverAddress("湖北省武汉市汉阳区.....");
        newOrder.setReceiverName("周敦鹏");
        newOrder.setReceiverMobile("1587174....");
        // 设置邮费
        newOrder.setPostAmount(postAmount);
        // 支付方式
        newOrder.setPayMethod(payMethod);
        // 备注
        newOrder.setLeftMsg(leftMsg);
        newOrder.setIsComment(YesOrNo.NO.type);
        newOrder.setIsDelete(YesOrNo.NO.type);
        newOrder.setCreatedTime(LocalDateTime.now());
        newOrder.setUpdatedTime(LocalDateTime.now());

        // 2. 循环根据itemSpecIds保存订单商品信息表
        Integer totalAmount = 0;    // 商品原价累计
        Integer realPayAmount = 0;  // 优惠后的实际支付价格累计

        for (SubmitItemSpecBO itemSpec : itemSpecs) {
            // TODO 整合redis后，商品购买的数量重新从redis的购物车中获取
            int buyCounts = itemSpec.getBuyCounts();

            //  2.1 根据规格id，查询规格的具体信息，主要获取价格
            ItemsSpec itemsSpec = itemsSpecMapper.selectById(itemSpec.getItemSpecId());
            totalAmount += itemsSpec.getPriceNormal() * buyCounts;
            realPayAmount += itemsSpec.getPriceDiscount() * buyCounts;
            // 2.2 根据商品id，获得商品信息以及商品图片
            String itemId = itemsSpec.getItemId();
            Items item = itemsMapper.selectById(itemId);
            // String imgUrl = itemService.queryItemMainImgById(itemId);

            // 2.3 循环保存子订单数据到数据库
            String subOrderId = sid.nextShort();
            OrderItems subOrderItem = new OrderItems();
            subOrderItem.setId(subOrderId);
            subOrderItem.setOrderId(orderId);
            subOrderItem.setItemId(itemId);
            subOrderItem.setItemName(item.getItemName());
            subOrderItem.setItemImg("http://...........");
            subOrderItem.setBuyCounts(buyCounts);
            subOrderItem.setItemSpecId(itemsSpec.getId());
            subOrderItem.setItemSpecName(itemsSpec.getName());
            // 订单实际支付的价格就是当前商品规格中的价格(优惠价)
            subOrderItem.setPrice(itemsSpec.getPriceDiscount());
            orderItemsMapper.insert(subOrderItem);
        }

        // 添加主订单信息
        newOrder.setTotalAmount(totalAmount);
        newOrder.setRealPayAmount(realPayAmount);
        // 添加主订单信息
        ordersMapper.insert(newOrder);

        // TODO 构建支付需要的表单数据
        return "creat order success";
    }
}
